package org.agamotto.cloud.template.controller;


import lombok.extern.slf4j.Slf4j;
import org.agamotto.cloud.template.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private TemplateService templateService;




    @GetMapping("/java/{projectName}")
    public Mono<Void> getJavaTemplate(@PathVariable("projectName")String projectName, ServerWebExchange exchange) {
        //response.writeTo()return templateService.getJavaTemplate(projectName,request.)
        DefaultDataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
        templateService.getJavaTemplate(projectName,buffer.asOutputStream());
        ServerHttpResponse response = exchange.getResponse();
       return response.writeWith(Mono.just(buffer));

    }
}
